package service

import (
	"context"
	"douyin_mini/cmd/comment/dal/db"
	"douyin_mini/cmd/comment/pack"
	"douyin_mini/cmd/comment/rpc"
	"douyin_mini/kitex_gen/comment"
	"douyin_mini/kitex_gen/user"
)

type CommentListService struct {
	ctx context.Context
}

func NewCommentListService(ctx context.Context) *CommentListService {
	return &CommentListService{ctx: ctx}
}

func (s *CommentListService) ListComments(req *comment.CommentListRequest) ([]*comment.Comment, error) {
	rawCommentsList, err := db.GetCommentsList(s.ctx, req.VideoId)
	if err != nil {
		return nil, err
	}
	uIds := pack.UserIds(rawCommentsList)
	comments := pack.Comments(rawCommentsList)
	users, err := rpc.GetUser(s.ctx, &user.UserInfoRequest{UserIds: uIds})
	if err != nil {
		return nil, err
	}
	for i := 0; i < len(rawCommentsList); i++ {
		commentUser := &comment.User{
			Id:            users[i].Id,
			Name:          users[i].Name,
			FollowCount:   users[i].FollowCount,
			FollowerCount: users[i].FollowerCount,
			IsFollowed:    users[i].IsFollowed,
		}
		comments[i].User = commentUser
	}
	return comments, nil
}
